<template>
  <div class="global-header"></div>
  <a-row :wrap="false">
    <a-col flex="200px">
      <RouterLink to="/">
        <div class="title-bar">
          <img class="logo" src="../assets/logo.png" alt="logo" />
          <div class="title">手势识别系统</div>
        </div>
      </RouterLink>
    </a-col>
    <a-col flex="auto">
      <a-menu
        v-model:selectedKeys="current"
        mode="horizontal"
        :items="items"
        @click="doMuneclick"
      />
    </a-col>
    <a-col flex="120px">
      <div class="user-login-status">
        <div v-if="loginUserStore.loginUser.id">
          <a-dropdown>
            <a-space>
              <a-avatar :src="loginUserStore.loginUser.userAvatar" />
              {{ loginUserStore.loginUser.userName ?? `无名` }}
            </a-space>
            <template #overlay>
              <a-menu>
                <a-menu-item>
                  <router-link to="/my_space">
                    <UserOutlined />
                    我的空间
                  </router-link>
                </a-menu-item>
                <a-menu-item @click="doLogout">
                  <LogoutOutlined />
                  退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
        <div v-else>
          <a-button type="primary" href="/user/login">登录</a-button>
        </div>
      </div>
    </a-col>
  </a-row>
</template>
<script lang="ts" setup>
import { h, ref } from 'vue'
import { HomeOutlined, LogoutOutlined, UserOutlined } from '@ant-design/icons-vue'
import { message, type MenuProps } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { computed } from 'vue'
import MySpacePage from '@/pages/MyspacePage.vue'
const router = useRouter()
const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/about',
    label: '图集展示',
    title: '图集展示',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/add_picture',
    label: '上传图片',
    title: '上传图片',
  },
  {
    key: '/admin/pictureManage',
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/shitou',
    label: '石头剪刀布',
    title: '石头剪刀布',
  },
  {
    key: '/video',
    label: '实时识别',
    title: '实时识别',
  },
  {
    key: '/ai',
    label: 'AI对话',
    title: 'AI对话',
  },
  {
    key: '/add_picture/batch',
    label: '批量上传',
    title: '批量上传',
  },
  {
    key: '/admin/spaceManage',
    label: '空间管理',
    title: '空间管理',
  },
]

const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    if (menu?.key?.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}
const items = computed(() => {
  return filterMenus(originItems)
})
const doMuneclick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  })
}
const current = ref<string[]>([])
router.afterEach((to, from, next) => {
  current.value = [to.path]
})
import { useLoginUserStore } from '../stores/UserLoginInStores'
const loginUserStore = useLoginUserStore()
import { userLogoutUsingPost } from '../api/userController'
const doLogout = async () => {
  const res = await userLogoutUsingPost()
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出成功')
    await router.push('/user/login')
  } else {
    message.error('退出失败' + res.data.message)
  }
}
</script>
<style scoped>
/* 移除顶部色条 */
.global-header {
  height: 0;
}

/* 顶部栏整体样式 */
a-row {
  padding: 12px 32px;
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
}

/* logo + 标题 */
.title-bar {
  display: flex;
  align-items: center;
}

.logo {
  height: 48px;
  margin-right: 12px;
  transition: transform 0.3s ease;
}
.logo:hover {
  transform: scale(1.06);
}

.title {
  color: #1f1f1f;
  font-size: 22px;
  font-weight: 600;
  font-family: 'Segoe UI', sans-serif;
}

/* 顶部菜单 */
::v-deep(.ant-menu-horizontal) {
  line-height: 56px;
  font-size: 16px;
  font-weight: 500;
  border-bottom: none;
  background-color: transparent;
}
::v-deep(.ant-menu-item) {
  padding: 0 20px;
}

/* 用户登录状态 */
.user-login-status {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 12px;
}

.user-login-status .ant-avatar {
  border: 2px solid #1890ff;
}

.user-login-status .ant-btn-primary {
  background-color: #1890ff;
  border: none;
  font-weight: bold;
}

.user-login-status .ant-btn-primary:hover {
  background-color: #1677d1;
}

/* 响应式支持 */
@media (max-width: 768px) {
  .title {
    font-size: 18px;
  }
  ::v-deep(.ant-menu-horizontal) {
    font-size: 14px;
  }
}
</style>
